package com.ruoyi.plus.api;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.api.ApiResult;
import com.ruoyi.common.core.api.BaseApi;
import com.ruoyi.common.core.api.version.ApiVersion;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * @author likun
 * 文件上传通用接口API
 */
@Slf4j
@Api(tags = "FileUploadApi-文件上传通用接口API")
@ApiVersion()
@RestController
@RequestMapping("app/api/plus/fileUploadApi")
public class FileUploadApi extends BaseApi {

    @ApiOperation(value = "upFile-上传文件(单个)", notes = "参数名: file")
    @Log(title = "上传文件(单个)", businessType = BusinessType.INSERT)
    @PostMapping(value = {"/uploadFile", "/upFile"})
    @ResponseBody
    public ApiResult<String> uploadFile(@RequestParam("file") MultipartFile file) {
        try {
            if (file.isEmpty()) {
                return new ApiResult<String>().setCode(-1).setMsg("上传失败,未接收到文件!").setData(null).setApiVersion(this.getApiVersion());
            }
            String url = FileUploadUtils.upload(file);
            return new ApiResult<String>().setMsg("上传成功!").setData(url).setApiVersion(this.getApiVersion());
        } catch (Exception e) {
            log.error("上传失败！", e);
            return new ApiResult<String>(getApiVersion()).setCode(-1).setMsg("上传失败! " + e.getMessage()).setData(null).setApiVersion(this.getApiVersion());
        }
    }


    @ApiOperation(value = "upFiles-上传文件(多个)", notes = "参数名: files[], 注意必须包含中括号")
    @Log(title = "upFiles-上传文件(多个)", businessType = BusinessType.INSERT)
    @PostMapping(value = {"/uploadFiles", "/upFiles"})
    @ResponseBody
    public ApiResult<String> uploadFiles(@RequestParam("files[]") List<MultipartFile> files) {
        if (files == null || files.isEmpty()) {
            return new ApiResult<String>(getApiVersion()).setMsg("批量上传失败,未接收到文件!").setCode(-1).setApiVersion(this.getApiVersion());
        }

        List<String> urlList = new ArrayList<>();
        for (MultipartFile file : files) {
            try {
                if (!file.isEmpty()) {
                    String url = FileUploadUtils.upload(file);
                    urlList.add(url);
                }
            } catch (Exception e) {
                log.error("批量上传失败！", e);
                new ApiResult<List<String>>(getApiVersion()).setMsg("批量上传失败,错误信息:" + e.getMessage()).setCode(-1).setData(null).setApiVersion(this.getApiVersion());
            }
        }
        String urls ="";
        if (!urlList.isEmpty()) {
            urls = StringUtils.join(urlList, ",");
        }
        return new ApiResult<String>(getApiVersion()).setMsg("批量上传成功!").setData(urls).setApiVersion(this.getApiVersion());
    }

}
